package com.ssy.lingxi.member.merchant.serviceimpl.feign;

import com.ssy.lingxi.component.rabbitMQ.service.IRabbitMQUtils;
import com.ssy.lingxi.member.merchant.api.model.constant.MemberProcessTypeEnum;
import com.ssy.lingxi.member.merchant.model.constant.MemberInnerStatusEnum;
import com.ssy.lingxi.member.merchant.model.constant.PlatformInnerStatusEnum;
import com.ssy.lingxi.member.merchant.service.feign.IReportFeignService;
import com.ssy.lingxi.member.merchant.utils.SecurityStringUtils;
import com.ssy.lingxi.report.api.constant.ReportConstants;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.feign.ReportControllerFeign;
import com.ssy.lingxi.report.api.vo.request.AddSubMemberVO;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeAddVO;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 调用报表服务Feign接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-11-20
 */
@Service
public class ReportFeignServiceImpl implements IReportFeignService {
    private static final Logger logger = LoggerFactory.getLogger(ReportFeignServiceImpl.class);

    @Resource
    private IRabbitMQUtils mqUtils;
    @Resource
    private ReportControllerFeign reportControllerFeign;

    /**
     * 通知报表服务统计会员审核步骤数据
     *
     * @param relationId         会员关系Id
     * @param memberId           上级会员Id
     * @param roleId             上级会员角色Id
     * @param lastInnerStatus    上一次的内部状态，如为0或Null表示不减少
     * @param currentInnerStatus 上一次的内部状态，如为0或Null表示不增加
     */
    @Async
    @Override
    public void reportMemberValidate(Long relationId, Long memberId, Long roleId, Integer lastInnerStatus, Integer currentInnerStatus, Integer processType) {
        MemberInnerStatusEnum lastInnerStatusEnum = MemberInnerStatusEnum.parse(lastInnerStatus);
        MemberInnerStatusEnum currentInnerStatusEnum = MemberInnerStatusEnum.parse(currentInnerStatus);
        if ((lastInnerStatusEnum == null || lastInnerStatusEnum.getReportTypeCode().equals(0)) && (currentInnerStatusEnum == null || currentInnerStatusEnum.getReportTypeCode().equals(0))) {
            return;
        }

        OperateNoticeVO noticeVO = new OperateNoticeVO();
        noticeVO.setMemberId(memberId);
        noticeVO.setRoleId(roleId);
        if (processType.equals(MemberProcessTypeEnum.MEMBER_DEPOSITORY.getCode())) {
            noticeVO.setDataSource(OperateDataSourceEnum.MEMBER_IMPORT.getCode());
        } else {
            noticeVO.setDataSource(OperateDataSourceEnum.MEMBER_CHANGE.getCode());
        }
        noticeVO.setReduceOperateType(lastInnerStatusEnum == null ? 0 : lastInnerStatusEnum.getReportTypeCode());
        noticeVO.setReduceCount((lastInnerStatusEnum == null || lastInnerStatusEnum.getReportTypeCode().equals(0)) ? 0L : 1L);
        noticeVO.setIncreaseOperateType(currentInnerStatusEnum == null ? 0 : currentInnerStatusEnum.getReportTypeCode());
        noticeVO.setIncreaseCount((currentInnerStatusEnum == null || currentInnerStatusEnum.getReportTypeCode().equals(0)) ? 0L : 1L);
        noticeVO.setReduceDataId(relationId);
        noticeVO.setIncreaseDataId(relationId);
        noticeVO.setIncreaseTitle("");

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(noticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            String jsonMessage = SecurityStringUtils.serializeObject(addVO);
            logger.info("向报表服务发送审核数据 => " + jsonMessage);
            mqUtils.sendMsg(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, jsonMessage);
        } catch (Exception e) {
            logger.error("向报表服务发送审核数据错误：" + e.getMessage());
        }
    }

    /**
     * 通知报表服务统计平台会员审核步骤数据
     *
     * @param relationId                 平台会员关系Id
     * @param memberId                   上级会员Id
     * @param roleId                     上级会员角色Id
     * @param lastPlatformInnerStatus    上一次的内部状态，如为0或Null表示不减少
     * @param currentPlatformInnerStatus 上一次的内部状态，如为0或Null表示不增加
     */
    @Async
    @Override
    public void reportPlatformMemberValidate(Long relationId, Long memberId, Long roleId, Integer lastPlatformInnerStatus, Integer currentPlatformInnerStatus) {
        PlatformInnerStatusEnum lastInnerStatusEnum = PlatformInnerStatusEnum.parse(lastPlatformInnerStatus);
        PlatformInnerStatusEnum currentInnerStatusEnum = PlatformInnerStatusEnum.parse(currentPlatformInnerStatus);
        if ((lastInnerStatusEnum == null || lastInnerStatusEnum.getReportTypeCode().equals(0)) && (currentInnerStatusEnum == null || currentInnerStatusEnum.getReportTypeCode().equals(0))) {
            return;
        }

        OperateNoticeVO noticeVO = new OperateNoticeVO();
        noticeVO.setMemberId(memberId);
        noticeVO.setRoleId(roleId);
        noticeVO.setDataSource(OperateDataSourceEnum.PLATFORM_MEMBER_VERIFY.getCode());
        noticeVO.setReduceOperateType(lastInnerStatusEnum == null ? 0 : lastInnerStatusEnum.getReportTypeCode());
        noticeVO.setReduceCount((lastInnerStatusEnum == null || lastInnerStatusEnum.getReportTypeCode().equals(0)) ? 0L : 1L);
        noticeVO.setIncreaseOperateType(currentInnerStatusEnum == null ? 0 : currentInnerStatusEnum.getReportTypeCode());
        noticeVO.setIncreaseCount((currentInnerStatusEnum == null || currentInnerStatusEnum.getReportTypeCode().equals(0)) ? 0L : 1L);
        noticeVO.setReduceDataId(relationId);
        noticeVO.setIncreaseDataId(relationId);
        noticeVO.setIncreaseTitle("");

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(noticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            String jsonMessage = SecurityStringUtils.serializeObject(addVO);
            logger.info("向报表服务发送平台审核数据 => " + jsonMessage);
            mqUtils.sendMsg(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, jsonMessage);
        } catch (Exception e) {
            logger.error("向报表服务发送平台审核数据错误：" + e.getMessage());
        }
    }

    @Override
    public OperateNoticeVO buildIncreaseReportVO(Integer dataSource, Integer operateType, Long memberId, Long roleId) {
        return this.buildIncreaseReportVO(dataSource, operateType, memberId, roleId, 1);
    }

    @Override
    public OperateNoticeVO buildIncreaseReportVO(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer count) {
        OperateNoticeVO increaseOperateNotice = new OperateNoticeVO();
        increaseOperateNotice.setMemberId(memberId);
        increaseOperateNotice.setRoleId(roleId);
        increaseOperateNotice.setDataSource(dataSource);
        increaseOperateNotice.setIncreaseOperateType(operateType);
        increaseOperateNotice.setIncreaseCount(Long.valueOf(count));
        return increaseOperateNotice;
    }

    @Override
    public OperateNoticeVO buildReduceReportVO(Integer dataSource, Integer operateType, Long memberId, Long roleId) {
        return this.buildReduceReportVO(dataSource, operateType, memberId, roleId, 1);
    }

    @Override
    public OperateNoticeVO buildReduceReportVO(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer count) {
        OperateNoticeVO reduceOperateNotice = new OperateNoticeVO();
        reduceOperateNotice.setMemberId(memberId);
        reduceOperateNotice.setRoleId(roleId);
        reduceOperateNotice.setDataSource(dataSource);
        reduceOperateNotice.setReduceOperateType(operateType);
        reduceOperateNotice.setReduceCount(Long.valueOf(count));
        return reduceOperateNotice;
    }

    @Override
    public List<OperateNoticeVO> buildReduceAndIncreaseVOList(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer subDataSource, Integer subOperateType, Long subMemberId, Long subRoleId) {
        List<OperateNoticeVO> operateNoticeVOList = new ArrayList<>();
        // 减少
        OperateNoticeVO reduceOperateNotice = this.buildReduceReportVO(dataSource, operateType, memberId, roleId, 1);
        operateNoticeVOList.add(reduceOperateNotice);
        // 增加
        OperateNoticeVO increaseOperateNotice = this.buildIncreaseReportVO(subDataSource, subOperateType, subMemberId, subRoleId, 1);
        operateNoticeVOList.add(increaseOperateNotice);
        return operateNoticeVOList;
    }

    @Override
    public void executeIncreaseReport(Integer dataSource, Integer operateType, Long memberId, Long roleId) {
        this.executeIncreaseReport(dataSource, operateType, memberId, roleId, 1);
    }

    @Async
    @Override
    public void executeIncreaseReport(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer count) {
        // 增加
        OperateNoticeVO operateNoticeVO = this.buildIncreaseReportVO(dataSource, operateType, memberId, roleId, count);

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(operateNoticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            mqUtils.sendMsg(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, SecurityStringUtils.serializeObject(addVO));
        } catch (Exception e) {
            logger.error("首页通知报表服务错误：" + e.getMessage());
        }
    }


    @Override
    public void executeReduceReport(Integer dataSource, Integer operateType, Long memberId, Long roleId) {
        this.executeReduceReport(dataSource, operateType, memberId, roleId, 1);
    }

    @Async
    @Override
    public void executeReduceReport(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer count) {
        // 减少
        OperateNoticeVO operateNoticeVO = this.buildReduceReportVO(dataSource, operateType, memberId, roleId, count);

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(operateNoticeVO);
        addVO.setOperateNoticeVOList(null);
        try {
            mqUtils.sendMsg(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, SecurityStringUtils.serializeObject(addVO));
        } catch (Exception e) {
            logger.error("首页通知报表服务错误：" + e.getMessage());
        }
    }

    @Override
    public void executeReduceAndIncreaseReport(Integer dataSource, Integer operateType, Long memberId, Long roleId, Integer subDataSource, Integer subOperateType, Long subMemberId, Long subRoleId) {
        List<OperateNoticeVO> operateNoticeVOList = this.buildReduceAndIncreaseVOList(dataSource, operateType, memberId, roleId, subDataSource, subOperateType, subMemberId, subRoleId);
        this.executeBatchReport(operateNoticeVOList);
    }

    @Async
    @Override
    public void executeBatchReport(List<OperateNoticeVO> operateNoticeVOList) {
        if (CollectionUtils.isEmpty(operateNoticeVOList)) {
            return;
        }

        OperateNoticeAddVO addVO = new OperateNoticeAddVO();
        addVO.setOperateNoticeVO(null);
        addVO.setOperateNoticeVOList(operateNoticeVOList);
        try {
            mqUtils.sendMsg(ReportConstants.REPORT_SYS_OPERATE_NOTICE_EXCHANGE, ReportConstants.REPORT_SYS_OPERATE_NOTICE_ROUTINGKEY, SecurityStringUtils.serializeObject(addVO));
        } catch (Exception e) {
            logger.error("首页通知报表服务错误：" + e.getMessage());
        }
    }

    @Async
    @Override
    public void addSubMember(Long memberId , Long roleId, LocalDateTime dateTime){
        try {
            AddSubMemberVO vo=new AddSubMemberVO();
            vo.setMemberId(memberId);
            vo.setRoleId(roleId);
            vo.setDateTime(dateTime);
            reportControllerFeign.addSubMember(vo);
        } catch (Exception e) {
            logger.error("首页通知报表服务错误：" + e.getMessage());
        }
    }
}
